package com.bankcomm.iccard;

import java.text.ParseException;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import com.bankcomm.R;
import com.bankcomm.common.ConfigManager;
import com.bankcomm.common.LPMid;
import com.bankcomm.ui.EMPView;
import com.bankcomm.ui.specialwidget.LPDigitPasswordTextField;
import com.bankcomm.ui.views.BCMHomeView;
import com.bankcomm.ui.views.LPBottomBar;
import com.bankcomm.util.LPUtils;
import com.bankcomm.util.SecretUtil;
import com.rytong.tools.crypto.AESCipher;
import com.rytong.tools.httpconnect.WaitDialog;
import com.rytong.tools.utils.Utils;
import com.zc.card.AudioCardManager;
import com.zc.card.NFCCardManager;
import com.rytong.tools.utils.LPToastUtil;

/**
 * 
 * 电子现金圈存
 * 
 */
public class ICCard_Cash_Load_Activity extends BaseActivity implements OnClickListener {

	public ScrollView scroll;

	public LinearLayout layout_Input_Money;
	public Button btnSubMoney, btnMoneyReset;
	public TextView tvm_ICCard, tvm_AccountBalance, tvm_CardBalance;
	public EditText etm_StoreMoney;

	public LinearLayout layout_Input_Pwds;
	public TextView tvp_ICCard, tvp_AccountBalance, tvp_CardBalance, tvp_StoreMoney;
	public Button btnSubList, btnListReset;
	public EditText et_StorePwd;
	public boolean notAllowSub = false;
	boolean selfLocking = false;

	private LinearLayout layout_Result;
	private TextView tvResultNotice;
	private Button btnResultReturn;
	Button btnReturn, btnHome;
	public String readInfoPan;

	private Thread threadGetFiled;
	private boolean stopThread;
	private String Field55Reply;
	private String money;

	String field55;
	private String cardPAN;
	/** 交易流水号 **/
	private String serialNo;

	private String field55Str;
	/** 帐号 **/
	private String accountNo;
	/** 电子柜员号 **/
	private String eTeller;

	protected LinearLayout logLayout;

	private String balance;
	
	LPDigitPasswordTextField lpdptf;
	
	private SecretUtil util_;
	private LPMid mid_;
	
	private LPBottomBar bottomBar;
	private RelativeLayout topLayout;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.iccard_cash_load_layout);
		
		util_ = new SecretUtil();
		mid_ = LPMid.getInstance();
		bottomBar=(LPBottomBar)findViewById(R.id.bottom_bar);
		topLayout=(RelativeLayout)findViewById(R.id.titleLayout);
		
		scroll = (ScrollView) findViewById(R.id.cash_load_scroll);
		instantiateInputMoney();
		instantiateInputPwd();
		instantiateResult();
		instantiateBtn();
		scroll.addView(layout_Input_Money);
		setLayoutHeight();
		((LPBottomBar)findViewById(R.id.bottom_bar)).setSelectTab(2);
	}
	
	private void setLayoutHeight() {
		LPUtils.scaledTHeightRel(topLayout);
		LPUtils.setTitleButtonWH(btnReturn);
		LPUtils.setTitleButtonWH(btnHome);
		
	}
	/** 
	 * 初始化输入金额的页面
	 * **/
	public void instantiateInputMoney() {
		layout_Input_Money = (LinearLayout) LayoutInflater.from(ICCard_Cash_Load_Activity.this).inflate(
				R.layout.iccard_cashload_input_money_layout, null);
		btnSubMoney = (Button) layout_Input_Money.findViewById(R.id.inputpm_submitBtn);
		btnMoneyReset = (Button) layout_Input_Money.findViewById(R.id.inputpm_resetBtn);
		tvm_ICCard = (TextView) layout_Input_Money.findViewById(R.id.inputpm_iccardnum);
		tvm_AccountBalance = (TextView) layout_Input_Money.findViewById(R.id.inputpm_accountbalance);
		tvm_CardBalance = (TextView) layout_Input_Money.findViewById(R.id.inputpm_cardbalance);
		etm_StoreMoney = (EditText) layout_Input_Money.findViewById(R.id.inputm_etstoremoney);
		// etm_StoreMoney.addTextChangedListener(textWatcher);
		btnSubMoney.setOnClickListener(subMoneyListener);
		btnMoneyReset.setOnClickListener(subMoneyListener);
	}
	/** 
	 * 初始化输入密码页面
	 * **/
	public void instantiateInputPwd() {
		layout_Input_Pwds = (LinearLayout) LayoutInflater.from(ICCard_Cash_Load_Activity.this).inflate(
				R.layout.iccard_cash_load_makesure_layout, null);
		btnSubList = (Button) layout_Input_Pwds.findViewById(R.id.inputp_submitBtn);
		btnListReset = (Button) layout_Input_Pwds.findViewById(R.id.inputp_resetBtn);
		tvp_ICCard = (TextView) layout_Input_Pwds.findViewById(R.id.inputp_iccardnum);
		tvp_AccountBalance = (TextView) layout_Input_Pwds.findViewById(R.id.inputp_accountbalance);
		tvp_CardBalance = (TextView) layout_Input_Pwds.findViewById(R.id.inputp_cardbalance);
		tvp_StoreMoney = (TextView) layout_Input_Pwds.findViewById(R.id.inputp_storemoney);
		
		et_StorePwd = (EditText) layout_Input_Pwds.findViewById(R.id.inputp_etpwds);
		
		et_StorePwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
		et_StorePwd.setFocusable(false);

		btnSubList.setOnClickListener(subListListener);
		
		/*2013年6月4日19:44:57*/
		et_StorePwd.setOnTouchListener(onTouchEditListener);
		btnListReset.setOnClickListener(subListListener);
	}
	/** 
	 * 初始化结果显示页面
	 * **/
	public void instantiateResult() {
		layout_Result = (LinearLayout) LayoutInflater.from(ICCard_Cash_Load_Activity.this).inflate(R.layout.iccard_result_layout, null);
		logLayout = (LinearLayout) layout_Result.findViewById(R.id.contentloglist);
		tvResultNotice = (TextView) layout_Result.findViewById(R.id.tv_cash_load_result);
		btnResultReturn = (Button) layout_Result.findViewById(R.id.btn_return);
		btnResultReturn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ICCard_Cash_Load_Activity.this.finish();
			}
		});
	}
	/** 
	 * 初始化页面中的按钮
	 * **/
	public void instantiateBtn() {
		btnReturn = (Button) findViewById(R.id.backtitle);
		btnHome = (Button) findViewById(R.id.hometitle);
		
		btnReturn.setOnClickListener(this);
		btnHome.setOnClickListener(this);
	}

	TextWatcher textWatcher = new TextWatcher() {
		boolean isAllowInput = true;
		int deleteIndex = 0;

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			if (!s.toString().equals("")) {
				double inputMoney = Double.parseDouble(s.toString());
				if ((moneyCardBalance + inputMoney) > 1000) {
					sendError(getString(R.string.audiocard_notice));
					deleteIndex = s.toString().length() - 1;
					isAllowInput = false;
				} else {
					isAllowInput = true;
				}
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
			String str = s.toString();
			int slen = str.length();
			if (!isAllowInput) {
				s.delete(deleteIndex, slen);
			}
		}
	};
	
	
	
	/** 
	 * 触摸密码输入框时弹出自定义键盘
	 * **/
	OnTouchListener onTouchEditListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			int action = event.getAction();
//			showDialog();
			if (action == MotionEvent.ACTION_DOWN) {
				if(LPMid.getInstance().isShow){
					return true;
				}
				LPMid.getInstance().isShow=true;
				
				if (null != dlg) {
					if (!dlg.isShowing()) {
						mid_.digitStr=null;
						et_StorePwd.setText("");
						showKeyBoardByTag(et_StorePwd);
					}
				} else {
					mid_.digitStr=null;
					et_StorePwd.setText("");
					showKeyBoardByTag(et_StorePwd);
				}
				return true;
			}
			return true;
		}
	};

	/** 
	 * 圈存输入密码后提交
	 * **/
	OnClickListener subListListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

			if (v.getId() == btnSubList.getId()) {
				showDialog();
				// tvm_ICCard,tvm_AccountBalance,tvm_CardBalance
				String strPwd = et_StorePwd.getText().toString();
				if (!"".equals(strPwd)) {
					if (money != null) {
						threadGetFiled = new Thread(new Runnable() {

							@Override
							public void run() {
								if (null != manager) {
									if (manager instanceof AudioCardManager) {

										Field55Reply = ((AudioCardManager) manager).Build55Field(money);
										if (Field55Reply.length() == 2) {
											stopThread = false;
											sendError(getString(R.string.errorAudioInfo));
											closeDialog();
										} else {
											handlerField55(Field55Reply);
											stopThread = true;
										}
									}
								}

								if (null != parcelNfc) {
									Field55Reply = NFCCardManager.Build55Field(parcelNfc, money);

									if (Field55Reply.length() == 2) {
										stopThread = false;
										sendError(getString(R.string.errorAudioInfo));
										closeDialog();
									} else {
										handlerField55(Field55Reply);
										stopThread = true;
									}
								}

							}
						});
						threadGetFiled.start();
						new Thread(new Runnable() {

							@Override
							public void run() {
								try {
									threadGetFiled.join();
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								if (stopThread) {
									if (cardPAN.equals(readInfoPan)) {
										sendCashLoadUrl();
									} else {
										// "换卡了"
									}
								}
							}
						}).start();
					}
				} else {
					sendError(getString(R.string.pwdIsNullNotice));
					closeDialog();
				}
				// scroll.removeAllViews();
				// scroll.addView(layout_Input_Pwds);
			}
			if (v.getId() == btnListReset.getId()) {
				scroll.removeAllViews();
				layout_Input_Money = null;
				et_StorePwd.setText("");
				instantiateInputMoney();
				scroll.addView(layout_Input_Money);
				notAllowSub=false;
			}
		}
	};

	/** 
	 * 输入金额后跳转方法
	 * **/
	OnClickListener subMoneyListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

			if (v.getId() == btnSubMoney.getId()) {
				handleMoneyFormat();
			}

			if (v.getId() == btnMoneyReset.getId()) {
				ICCard_Cash_Load_Activity.this.finish();
			}
		}
	};

	InputFilter lengthfilter = new InputFilter() {
		public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
			if ("".equals(source.toString())) {
				return null;
			}
			String dValue = dest.toString();
			String[] splitArray = dValue.split("\\.");
			if (splitArray.length > 1) {
				String dotValue = splitArray[1];
				int diff = dotValue.length() + 1 - 2;
				if (diff > 0) {
					return source.subSequence(start, end - diff);
				}
			}
			return null;
		}
	};
	/** 
	 * 处理金额格式
	 * **/
	private void handleMoneyFormat() {
		showDialog();
		String strmoney = etm_StoreMoney.getText().toString();
		if (strmoney.endsWith(".")) {
			closeDialog();
			sendError(getString(R.string.not_overline));
			return;
		}
		if (strmoney.startsWith(".")) {
			closeDialog();
			sendError(getString(R.string.not_overline));
			return;
		}
		if (!"".equals(strmoney)) {
			if (strmoney.contains(".")) {
				String[] arrMoney = strmoney.split("\\.");
				int length = arrMoney[1].length();
				if (length > 2) {
					closeDialog();
					sendError(getString(R.string.not_overline));
					return;
				}
			}
			double input_Money = Double.parseDouble(strmoney);
			if (moneyCardBalance + input_Money > 1000.00) {
				closeDialog();
				sendError(getString(R.string.not_overline));
				return;
			}
			if (notAllowSub) {
				if (!mapSubMoney.isEmpty()) {
					saveAndSetValue();
				} else {
					closeDialog();
				}
			} else {
				closeDialog();
				sendError(getString(R.string.notice_inner_card));
			}
		} else {
			closeDialog();
			sendError(getString(R.string.mnullnotice));

		}
	}
	/** 
	 * 保存页面数据传到下一页面赋值
	 * **/
	protected void saveAndSetValue() {
		InputMethodManager imm = (InputMethodManager) ICCard_Cash_Load_Activity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(ICCard_Cash_Load_Activity.this.getCurrentFocus().getApplicationWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
		String tvm_ICCardStr = tvm_ICCard.getText().toString();
		String tvm_AccountBalanceStr = tvm_AccountBalance.getText().toString();
		String tvm_CardBalanceStr = tvm_CardBalance.getText().toString();
		String etm_StoreMoneyStr = etm_StoreMoney.getText().toString();

		tvp_ICCard.setText(tvm_ICCardStr);
		tvp_AccountBalance.setText(tvm_AccountBalanceStr);
		tvp_CardBalance.setText(tvm_CardBalanceStr);
		tvp_StoreMoney.setText(getString(R.string.storeMoney).concat(etm_StoreMoneyStr));

		money = CardHandlerUtils.handlerDecimalto12(etm_StoreMoneyStr);
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				if (!mapSubMoney.isEmpty()) {
					scroll.removeAllViews();
					scroll.addView(layout_Input_Pwds);
				}
				closeDialog();
			}
		});
	}
	/** 
	 * 发送圈存交易请求
	 * 出现写卡失败时进行抹帐请求
	 * **/
	protected void sendCashLoadUrl() {
		String tvp_ICCardStr = tvp_ICCard.getText().toString().split(":")[1];
		final String tvp_StoreMoneyStr = tvp_StoreMoney.getText().toString().split(":")[1];
		String etp_StorePwd = et_StorePwd.getText().toString();

		// String bodyNoEscape = "account=" + tvp_ICCardStr + "&amount=" + tvp_StoreMoneyStr + "&field55=" + field55 + "&cardSequence=001"
		// + "&ewp_tran_action=iccard_cash_load&password=" + LPUtils.escapeURIComponent(etp_StorePwd);
		try {
			/*2013年6月4日19:45:28*/
//			etp_StorePwd = encryptWithPasswordKey(etp_StorePwd);
			etp_StorePwd = SecretUtil.encryptValue(etp_StorePwd, null);
		} catch (Exception e1) {
			e1.printStackTrace();
			closeDialog();
			return;
		}

		String body = "account=" + Utils.escapeURIComponent(tvp_ICCardStr) + "&amount=" + Utils.escapeURIComponent(tvp_StoreMoneyStr)
				+ "&field55=" + Utils.escapeURIComponent(field55) + "&cardSequence=" + Utils.escapeURIComponent("001")
				+ "&ewp_tran_action=" + Utils.escapeURIComponent("iccard_cash_load") + "&password="
				+ Utils.escapeURIComponent(etp_StorePwd);
		// System.out.println("body___________________:" + bodyNoEscape);
//		body = DESCipher.doEncrypt(body, AESCipher.serverKey_);
		String url = ConfigManager.ICCARD_URL_S;
		String reply = "";
		try {
			body = AESCipher.encrypt(body, AESCipher.clientKey_, AESCipher.clientIv_);

			reply = (String) LPMid.getInstance().hm_.sendPostRequest(url, body, null, null, null);
//			reply = DESCipher.doDecrypt(reply, AESCipher.serverKey_);
			reply = AESCipher.decrypt(reply, AESCipher.serverKey_, AESCipher.serverIv_);
			Utils.printOutToConsole("圈存结果报文-------" + reply);
		} catch (Exception e) {
			closeDialog();
//			LPMid.getInstance().alert(ICCard_Cash_Load_Activity.this, e.getMessage(), false);
			handleException(e.getMessage());

		}
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				et_StorePwd.setText("");
			}
		});

		if (!reply.contains("error")) {
			handlerJsonSuccessResult(reply);
			if (accountNo != null) {
				int execResult = -2012;
				if (null != manager) {
					if (manager instanceof AudioCardManager) {
						//
						execResult = ((AudioCardManager) manager).ExecScript(field55Str);
					}
				}
				if (null != parcelNfc) {
					execResult = NFCCardManager.ExecScript(parcelNfc, field55Str);
				}
				if (execResult == 0) {
					// 跳转成功
					handler_Load_Success();

				} else if (execResult == 0x00000818) {
					// 抹帐
					sendRollBackRequest(serialNo);
					handler_Load_Failed(getString(R.string.notice_client_check), true);
				} else {
					handler_Load_Failed(getString(R.string.notice_client_check), true);
				}
			}

		} else {
			handler_Load_Failed(reply, false);
		}

		closeDialog();
	}

	/** 
	 * 处理圈存交易失败后页面显示
	 * @param str 错误信息
	 * @param isRollback  抹帐错误提示标识
	 * **/
	private void handler_Load_Failed(final String str, final boolean isRollback) {
		if (null != manager) {
			if (manager instanceof AudioCardManager) {
				((AudioCardManager) manager).ExecScript("");
			}
		}
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				CardListItem = (LinearLayout) LayoutInflater.from(ICCard_Cash_Load_Activity.this).inflate(R.layout.iccard_result_errorcode,
						null);
				TextView tvErrorCode = (TextView) CardListItem.findViewById(R.id.errorcode);
				TextView tvErrorInfo = (TextView) CardListItem.findViewById(R.id.errorinfo);
				if (isRollback) {
					tvErrorCode.setText(str);
					tvErrorInfo.setVisibility(View.GONE);
					logLayout.addView(CardListItem);
				} else {
					String errorInfo = null;
					try {
						JSONObject jo = new JSONObject(str);
						errorInfo = jo.getString("error");
					} catch (JSONException e) {
						e.printStackTrace();
					}

					if (errorInfo != null) {
						String[] errinfo = errorInfo.split("\\|");

						tvErrorCode.setText(getString(R.string.errorCode) + errinfo[0]);
						tvErrorInfo.setText(errinfo[1] + getString(R.string.notice_client_check));
						logLayout.addView(CardListItem);
					}
				}
				scroll.removeAllViews();
				tvResultNotice.setText(getString(R.string.load_failed));
				scroll.addView(layout_Result);
			}
		});

	}

	/** 
	 * 处理圈存成功后的页面显示
	 * **/
	private void handler_Load_Success() {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				scroll.removeAllViews();
				instantiateResult();
				tvResultNotice.setText(getString(R.string.load_success));
				CardListItem = (LinearLayout) LayoutInflater.from(ICCard_Cash_Load_Activity.this)
						.inflate(R.layout.iccard_result_item, null);
				TextView iccardId = (TextView) CardListItem.findViewById(R.id.iccardNum);
				TextView accountbalance = (TextView) CardListItem.findViewById(R.id.accountbalance);
				TextView storeMoney = (TextView) CardListItem.findViewById(R.id.storeMoney);
				iccardId.setText(getString(R.string.iccardNum).concat(accountNo));
				accountbalance.setText(getString(R.string.accountbalance).concat(balance));
				try {
					storeMoney.setText(getString(R.string.storeMoney).concat(CardHandlerUtils.handler12toDecimal(money)));
				} catch (ParseException e) {
					e.printStackTrace();
				}

				logLayout.addView(CardListItem);
				scroll.addView(layout_Result);
			}
		});

	}

	/** 
	 * 发送抹帐请求
	 * @param serialNo2 抹帐所需圈存成功的序列号
	 * **/
	private void sendRollBackRequest(String serialNo2) {
		String url = ConfigManager.ICCARD_URL_S;
		String body = "ewp_tran_action=" + Utils.escapeURIComponent("iccard_tran_cancel") + "&serialNo="
				+ Utils.escapeURIComponent(serialNo2) + "&eTeller=" + Utils.escapeURIComponent(eTeller);
//		body = DESCipher.doEncrypt(body, AESCipher.serverKey_);
		
		try {
			body = AESCipher.encrypt(body, AESCipher.clientKey_, AESCipher.clientIv_);
			
			String str = (String) LPMid.getInstance().hm_.sendPostRequest(url, body, null, null, null);
//			str = DESCipher.doDecrypt(str, AESCipher.serverKey_);
			str = AESCipher.decrypt(str, AESCipher.serverKey_, AESCipher.serverIv_);
			
			Utils.printOutToConsole("抹帐结果报文：----" + str);
			// if (str.equals("success")) {
			// str = getString(R.string.rollback_success);
			// } else {
			// str = getString(R.string.rollback_failed);
			// }
			// sendError(str);
		} catch (Exception e) {
//			LPMid.getInstance().alert(ICCard_Cash_Load_Activity.this, e.getMessage(), false);
			handleException(e.getMessage());
		}

	}

	/**
	 * {serialNo:100000000441034, accountNo:6222620110003251817, accountName:儿童卡一, balance:20358.10, tranAuthCode:224092,
	 * field55:910A767E9CE875DEC5E8303072199F180400022409861004DA9F790A0000000001007E8B1F7500, totalFee:0.00, tranBankFee:0.00,
	 * inBankFee:0.00, outBankFee:0.00}
	 * 处理圈存成功的结果
	 * @param reply 圈存成功返回值
	 */
	private void handlerJsonSuccessResult(String reply) {
		try {
			JSONObject replyJO = new JSONObject(reply);
			serialNo = replyJO.getString("serialNo");
			field55Str = replyJO.getString("field55");
			accountNo = replyJO.getString("accountNo");
			balance = replyJO.getString("balance");
			eTeller = replyJO.getString("eTeller");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return super.onTouchEvent(event);
	}
	
	/** 
	 * 显示自定义键盘
	 * 控制必须在请求到随机数时显示键盘
	 * @param edit 点击的密码框
	 * **/
	public void showKeyBoard(final EditText edit) {
		Runnable randomKeyThread = new Runnable() {

			@Override
			public void run() {
				synchronized (ICCard_Cash_Load_Activity.this) {
					util_.getRadomKey(mid_);
//					getRandomKey();
				}
			}

		};

		final Thread threadRandomKey = new Thread(randomKeyThread);
		threadRandomKey.start();
		runOnUiThread(new Runnable() {
			public void run() {
				try {
					threadRandomKey.join();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(!mid_.notShowDialog){
				closeDialog();
				util_.OnCreateInputWindow(ICCard_Cash_Load_Activity.this, edit, dlg);
				}else{
					closeDialog();
					mid_.notShowDialog=false;
					LPMid.getInstance().isShow=false;
					LPToastUtil.getInstance().getToast(getString(R.string.notice_retry),ICCard_Cash_Load_Activity.this);
				}
//				OnCreateInputWindow(edit);
			}
		});

	}
	
	public void showKeyBoardByTag(final EditText edit){
		WaitDialog wd = new WaitDialog();
		wd.addFgTask(ICCard_Cash_Load_Activity.this, null, 0, 0, 0, new WaitDialog.Task(0) {

			@Override
			public void run(WaitDialog dlg) throws Exception {
				util_.getRadomKey(mid_);
			}
			
			@Override
			public void onSuccess(WaitDialog dia) {
				super.onSuccess(dia);
				if(!mid_.notShowDialog){
					runOnUiThread(new Runnable() {
						public void run() {
							util_.OnCreateInputWindow(ICCard_Cash_Load_Activity.this, edit, dlg);
						}
					});
					
					}else{
						mid_.notShowDialog=false;
						LPMid.getInstance().isShow=false;
						runOnUiThread(new Runnable() {
							public void run() {
						    LPToastUtil.getInstance().getToast(getString(R.string.notice_retry),ICCard_Cash_Load_Activity.this);
							}
						});
					}
			}
			
			@Override
			public void onFailure(WaitDialog dlg) {
				super.onFailure(dlg);
			}
			
		});	
	}
	/** 
	 * 处理55域
	 * **/
	public void handlerField55(String reply) {
		String[] fieldReply = reply.split("\\|");
		field55 = fieldReply[3];
		cardPAN = fieldReply[0];

	}

	@Override
	public void onClick(View v) {
//		Bundle inBundle = new Bundle();
		switch (v.getId()) {
		case R.id.backtitle:
			mid_.notShowDialog=false;
			this.finish();
			break;
		case R.id.hometitle:
//			LPMid.getInstance().um_.backToHome(this, 0);	
			mid_.notShowDialog=false;
			ConfigManager.currentView_.finish();
			BCMHomeView.changeScrollTab(2);
			this.finish();
			break;
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			if (keyCode == KeyEvent.KEYCODE_BACK) {
				hanlderBackPress();
				return true;
			}
		}
		return false;
	}
	/** 
	 * 集中处理返回按钮
	 * **/
	private void hanlderBackPress() {
		LinearLayout layout = (LinearLayout) scroll.getChildAt(0);
		if (layout == layout_Input_Money) {
			this.finish();
		}
		if (layout == layout_Input_Pwds) {
			scroll.removeAllViews();
			layout_Input_Money = null;
			instantiateInputMoney();
			et_StorePwd.setText("");
			scroll.addView(layout_Input_Money);
			notAllowSub=false;
		}
		if (layout == layout_Result) {
			this.finish();
		}
	}

	@Override
	public void finish() {
		super.finish();
		mid_.notShowDialog=false;
	}
	/** 
	 * 清除页面数据
	 * **/
	void cleanView() {
		tvm_ICCard.setText(getString(R.string.iccardNum));
		tvm_AccountBalance.setText(getString(R.string.accountbalance));
		tvm_CardBalance.setText(getString(R.string.cardbalance));
		et_StorePwd.setText("");

	}

}